package com.yy.jhl.ui.manager.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.foamtrace.photopicker.intent.PhotoPreviewIntent;
import com.yy.common.base.BaseActivity;
import com.yy.common.commonutils.ImageLoaderUtils;
import com.yy.common.commonutils.StringUtil;
import com.yy.common.commonwidget.OnNoDoubleClickListener;
import com.yy.jhl.R;
import com.yy.jhl.app.AppConstant;
import com.yy.jhl.bean.LogisticsInfo;
import com.yy.jhl.ui.main.activity.PhotosShowActivity;
import com.yy.jhl.ui.manager.adapter.PictureAdapter;
import com.yy.jhl.ui.manager.contract.OutboundCheckContract;
import com.yy.jhl.ui.manager.presenter.OutboundCheckPresenter;
import com.yy.jhl.video.MoviePlayActivity;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 出库准备 审核界面
 * Created by libingn on 2017/12/11.
 */

public class OutboundCheckActivity extends BaseActivity<OutboundCheckPresenter> implements OutboundCheckContract.View {

    @BindView(R.id.order_name_text)
    TextView orderNameText;
    @BindView(R.id.order_code_text)
    TextView orderCodeText;
    @BindView(R.id.product_text)
    TextView productText;
    @BindView(R.id.order_product_more_text)
    TextView productMoreText;
    @BindView(R.id.letter_parent)
    LinearLayout letterParent;
    @BindView(R.id.auth_letter_img)
    ImageView authLetterImg;
    @BindView(R.id.note_text)
    TextView noteText;
    @BindView(R.id.time_text)
    TextView timeText;
    @BindView(R.id.note_edit)
    EditText noteEdit;
    @BindView(R.id.again_btn)
    Button againBtn;
    @BindView(R.id.no_again_btn)
    Button noAgainBtn;
    @BindView(R.id.message_btn)
    Button messageBtn;
    LogisticsInfo logisticsInfo;
    @BindView(R.id.noScrollgridview)
    GridView noScrollgridview;
    PictureAdapter pictureAdapter;
    ArrayList<String> attachPaths = new ArrayList<>();
    public static void startAction(Context mContext, LogisticsInfo logisticsInfo) {
        Intent intent = new Intent(mContext, OutboundCheckActivity.class);
        intent.putExtra("logisticsInfo", logisticsInfo);
        mContext.startActivity(intent);
    }

    @Override
    public int getLayoutId() {
        return R.layout.act_outbound_check;
    }

    @Override
    public void initView() {
        logisticsInfo = (LogisticsInfo) getIntent().getSerializableExtra("logisticsInfo");
        showTitle("出库准备", new OnNoDoubleClickListener() {
            @Override
            protected void onNoDoubleClick(View v) {
                finish();
            }
        });
        orderNameText.setText(logisticsInfo.getTitle());
        orderCodeText.setText(logisticsInfo.getOrder_sn());
        String batchInfo = logisticsInfo.getBatch_info().getItems();
        int batchInfoTotal = logisticsInfo.getBatch_info().getTotal();
        if (batchInfoTotal > 2){
            productMoreText.setVisibility(View.VISIBLE);
        }else {
            productMoreText.setVisibility(View.GONE);
        }
        productText.setText(StringUtil.isEmpty(batchInfo) ? "" : batchInfo.replace("--","\n"));
        String attach = logisticsInfo.getAttach();
//        if (StringUtil.isEmpty(attach)) {
//            letterParent.setVisibility(View.GONE);
//        } else {
//            letterParent.setVisibility(View.VISIBLE);
//            ImageLoaderUtils.display(mContext, authLetterImg, attach);
//        }
        noteText.setText(logisticsInfo.getRemark());
        timeText.setText(StringUtil.isEmpty(logisticsInfo.getUpdate_time()) ? logisticsInfo.getCreate_time() :logisticsInfo.getUpdate_time() );
        pictureAdapter = new PictureAdapter(mContext,false);
        noScrollgridview.setAdapter(pictureAdapter);
        noScrollgridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                                    long arg3) {
                if (arg2 == attachPaths.size()) {
                }else if (attachPaths.get(arg2).endsWith("mp4")){
                    //小视频
                    MoviePlayActivity.startAction(mContext,attachPaths.get(arg2));
                }else {
                    //预览
                    PhotosShowActivity.startAction(mContext,attachPaths,arg2);
                }
            }
        });
        if (!StringUtil.isEmpty(attach)){
            String[] arrays = attach.split("\\#\\*\\#");
            for (int i =0;i<arrays.length;i++){
                attachPaths.add(arrays[i]);
            }
            pictureAdapter.setSelectPaths(attachPaths);
            StringUtil.setGridViewHeightBasedOnChildren(noScrollgridview,arrays.length,4);
        }
        productMoreText.setOnClickListener(new OnNoDoubleClickListener() {
            @Override
            protected void onNoDoubleClick(View v) {
                ProductListViewWebViewActivity.startAction(mContext,logisticsInfo.getOrder_id(),logisticsInfo.getBatch_id());
            }
        });
    }

    @OnClick({R.id.again_btn, R.id.no_again_btn, R.id.message_btn})
    public void onViewClicked(View view) {
        int status = 1;
        switch (view.getId()) {
            case R.id.again_btn:
                status = 2;
                break;
            case R.id.no_again_btn:
                status = 3;
                break;
            case R.id.message_btn:
                status = 4;
                break;
        }
        String remark = noteEdit.getText().toString().trim();
        if (StringUtil.isEmpty(remark)) {
            showShortToast("请输入备注信息！");
            return;
        }
        mPresenter.outboundSubmit(logisticsInfo.getBatch_id(), 3, status, remark);
    }

    @Override
    public void outboundSubmitSuccess() {
        finish();
    }

    @Override
    public void outboundSubmitFail() {

    }
}
